package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.dao.BlogCategoryDao;
import com.have.blog.admin.domain.dto.BlogCategoryDto;
import com.have.blog.admin.service.AdminBlogCategoryService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.BlogCategoryMapper;
import com.have.blog.model.mapper.BlogMapper;
import com.have.blog.model.models.BlogCategory;
import com.have.blog.model.models.BlogCategoryExample;
import com.have.blog.model.models.BlogExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminBlogCategoryServiceImpl implements AdminBlogCategoryService {

    @Autowired
    private BlogCategoryMapper blogCategoryMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogCategoryDao blogCategoryDao;

    @Override
    public List<BlogCategoryDto> getList(BlogCategoryDto blogCategoryDto) {
        PageHelper.startPage(blogCategoryDto.getPageNum(), blogCategoryDto.getPageSize());
        BlogCategoryExample example = new BlogCategoryExample();
        BlogCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (StringUtils.isNotEmpty(blogCategoryDto.getKeyword()) && StringUtils.isNotEmpty(blogCategoryDto.getKeyword().trim())) {
            criteria.andCategoryNameLike("%" + blogCategoryDto.getKeyword() + "%");
        }
        if(StringUtils.isNotEmpty(blogCategoryDto.getOrderByAscColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(blogCategoryDto.getOrderByAscColumn())).toString();
            example.setOrderByClause(column + " asc");
        } else if (StringUtils.isNotEmpty(blogCategoryDto.getOrderByDescColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(blogCategoryDto.getOrderByDescColumn())).toString();
            example.setOrderByClause(column + " desc");
        } else {
            example.setOrderByClause("sort desc");
        }
        List<BlogCategory> list = blogCategoryMapper.selectByExample(example);
        List<BlogCategoryDto> resultList = MyBeanUtils.copyListProperties(list, BlogCategoryDto::new);
        return resultList;
    }

    @Override
    public Wrapper addBlogCategory(BlogCategoryDto blogSortDto) {
        BlogCategoryExample example = new BlogCategoryExample();
        example.createCriteria()
                .andCategoryNameEqualTo(blogSortDto.getCategoryName())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        long count = blogCategoryMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }
        BlogCategory blogSort = new BlogCategory();
        MyBeanUtils.copyProperties(blogSortDto, blogSort);
        blogSort.setStatus(BlogStatus.ENABLE.getStatus());
        blogSort.setClickCount(0);
        int insert = blogCategoryMapper.insert(blogSort);
        if (insert > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public Wrapper editBlogCategory(BlogCategoryDto blogSortDto) {
        BlogCategory blogSort = blogCategoryMapper.selectByPrimaryKey(blogSortDto.getUid());
        if (blogSort == null) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_NOT_EXIST);
        }
        blogSort.setStatus(BlogStatus.ENABLE.getStatus());
        blogSort.setCategoryName(blogSortDto.getCategoryName());
        blogSort.setSort(blogSortDto.getSort());
        blogSort.setContent(blogSortDto.getContent());
        int update = blogCategoryMapper.updateByPrimaryKeySelective(blogSort);
        if (update > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchBlogCategory(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }

        BlogExample example = new BlogExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andBlogCategoryUidIn(uids);
        long count = blogMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.BLOG_UNDER_THIS_SORT);
        }

        BlogCategory blogSort = new BlogCategory();
        blogSort.setStatus(BlogStatus.DISABLE.getStatus());
        BlogCategoryExample sortExample = new BlogCategoryExample();
        sortExample.createCriteria()
                .andUidIn(uids);
        int delete = blogCategoryMapper.updateByExampleSelective(blogSort, sortExample);
        if (delete > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }

    @Override
    public Wrapper stickBlogCategory(BlogCategoryDto blogCategoryDto) {
        PageHelper.startPage(1, 1);
        BlogCategoryExample sortExample = new BlogCategoryExample();
        sortExample.setOrderByClause("sort desc");
        List<BlogCategory> tBlogSorts = blogCategoryMapper.selectByExample(sortExample);
        if (CollectionUtils.isEmpty(tBlogSorts)) {
            return Wrapper.success();
        }
        BlogCategory maxSort = tBlogSorts.get(0);
        if (maxSort.getUid().equals(blogCategoryDto.getUid())) {
            return Wrapper.successWithMessage(AdminMessageConstant.THIS_SORT_IS_TOP);
        }
        BlogCategory blogSort = blogCategoryMapper.selectByPrimaryKey(blogCategoryDto.getUid());
        blogSort.setSort(maxSort.getSort() + 1);
        int update = blogCategoryMapper.updateByPrimaryKeySelective(blogSort);
        if (update > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Transactional
    @Override
    public Wrapper sortBlogCategoryByClickCount() {
        int count = blogCategoryDao.setSortByClickCount();
        if (count > 0) {
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Transactional
    @Override
    public Wrapper sortBlogCategoryByBlogCount() {
        blogCategoryDao.sortBlogCategoryByBlogCount();
        return Wrapper.success();
    }
}
